package com.sxl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxl.config.LayUiResult;
import com.sxl.domain.Modules;
import com.sxl.domain.Rolemodules;
import com.sxl.service.ModulesService;
import com.sxl.service.RolemodulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/4/18 16:22
 */
@RequestMapping("/modules")
@RestController
@CrossOrigin
public class ModulesController {
    @Autowired
    private ModulesService modulesService;
    @Autowired
    private RolemodulesService rolemodulesService;
    /**
     * 查询所有数据
     * @return
     * 树形表格的数据
     */
    @GetMapping
    public Map<String,Object> selectAllModules(){
        List<Modules> list = modulesService.list();
        return LayUiResult.toClient("0", "success",modulesService.count(),list);
    }

    /**
     * 查询该分类是否有子分类
     * @return
     */
    @PostMapping("/findOneLevel")
    public List<Modules> findOneLevel(){
       return modulesService.findOneLevel();
    }

    /**
     * 查询该分类是否有子分类
     * @param id
     * @return
     */
    @PostMapping("/findIsHvaeSon")
    public boolean findIsHvaeSon(Integer id){
        return modulesService.findIsHvaeSon(id) >0 ? true : false;
    }

    /**
     * 查询该分类是否有产品
     * @param id
     * @return
     */
    @PostMapping("/findIsHaveModules")
    public boolean findIsHaveModules(Integer id){
        return modulesService.findIsHaveModules(id) >0 ? true : false;
    }

    /**
     * 添加
     * @param modules
     * @return
     */
    @PostMapping("/insertModules")
    public Map<String,Object> insertModules(Modules modules){
        if (modules.getParentId()==-1){
            List<Modules> list = modulesService.selectAllByParentId(modules.getParentId());
            for (int i = 0;i<list.size();i++){
                if (modules.getName().equals(list.get(i).getName())){
                    return LayUiResult.toClient("1","同一个父节点下节点名称不能相同");
                }
            }
        }else{
            List<Modules> list = modulesService.selectAllByParentId(modules.getParentId());
            for (int i = 0;i<list.size();i++){
                if (modules.getName().equals(list.get(i).getName())){
                    return LayUiResult.toClient("1","同一个子节点下节点名称不能相同");
                }
            }
        }
        boolean rel = modulesService.save(modules);
        return rel== true? LayUiResult.toClient("0", "添加成功") : LayUiResult.toClient("1", "添加失败");
    }
    /**
     *修改
     * @param modules
     * @return
     */
    @PostMapping("/updateModules")
    public Map<String,Object> updateModules(Modules modules){
        List<Modules> list = modulesService.list();
        Modules m = modulesService.getById(modules.getId());
        if (!modules.getName().equals(m.getName())){
            for (int i=0;i<list.size();i++){
                if(modules.getName().equals(list.get(i).getName())){
                    return LayUiResult.toClient("0", "已有模块使用该名称");
                }
            }
        }
        boolean rel = modulesService.updateById(modules);
        return rel== true? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/deleteModules")
    public boolean deleteModules(Integer id){
        return modulesService.removeById(id);
    }

    /**
     * 树状图
     */
    @PostMapping("/getDendrogram")
    public List<Modules> getMenu(Integer roleId){
        //回显当前角色的权限
        QueryWrapper<Rolemodules> wrapper=new QueryWrapper<>();
        wrapper.eq("roleId",roleId);
        List<Rolemodules> currList=rolemodulesService.list(wrapper);

        //1、查询所有菜单数据
        List<Modules> allMenu=modulesService.list();
        //2、获取所有的一级菜单
        List<Modules> rootMenu=new ArrayList<>();
        for(Modules m:allMenu){
            if (m.getParentId()==-1){
                rootMenu.add(m);
            }
        }
        //3、设置一级菜单的子菜单
        for(Modules m: rootMenu){
            //调用递归
            m.setChildren(getChildren(m.getId(),allMenu,currList));
            //已有的权限设置勾选
            for (Rolemodules rm : currList){
                if (m.getId().equals(rm.getModuleId())){
                    m.setChecked(true);
                    m.setSpread(true);
                }
            }
        }
        return rootMenu;
    }
    /**
     * 递归查找子菜单
     * @param pid
     * @param all
     * @return
     */
    private List<Modules> getChildren(Integer pid,List<Modules> all, List<Rolemodules> currList){
        List<Modules> children=new ArrayList<>();//子菜单集合
        //、在所有菜单里面找父id=pid的
        for (Modules m:all){
            if(m.getParentId()==pid){
                children.add(m);
            }
        }
        //2、递归找子的子....
        for (Modules m:children){
            m.setChildren(getChildren(m.getId(),all,currList));
            //已有权限设置勾选
            for (Rolemodules rm : currList){
                if (m.getId()==rm.getModuleId()){
                    m.setChecked(true);
                    m.setSpread(true);
                }
            }
        }
        //3、递归退出
        if (children.size()==0){
            return new ArrayList<>();
        }
        return  children;
    }

    /**
     * 角色管理 保存权限
     * @param roleId  当前角色id
     * @param selectedModule
     * @return
     */
    @PostMapping("/savePermission")
    public Boolean savePermission(Integer roleId,@RequestParam("selectedModule") List<Integer> selectedModule){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("roleId",roleId);
        boolean remove = rolemodulesService.remove(wrapper);
        if(selectedModule.size()>0){
            List<Rolemodules> batchSaveList = new ArrayList<>();
            for (Integer mid : selectedModule){
                Rolemodules r = new Rolemodules();
                r.setRoleId(roleId);
                r.setModuleId(mid);
                batchSaveList.add(r);
            }
            return rolemodulesService.saveBatch(batchSaveList);
        }
        return remove;
    }
}